﻿package idv.cjcat.monica.platformer.player {
	import idv.cjcat.rusher.engine.logger.LogType;
	import idv.cjcat.rusher.geom.Vec2D;
	
	internal class RunningState extends PlayerState {
		
		public function RunningState(controller:PlayerController, direction:Number) {
			super(controller, direction);
			controller.engine.logger.log("Entered state: RunningState", LogType.INFO, controller);
			
			this.controller.jumpCount = 0;
		}
		
		override public function startJump():void {
			if (controller.jumpCount < PlayerController.MAX_JUMPS) {
				setState(JumpingUpDashingState);
				setPlayerMovieClip(renderTarget.jumpingUpForward, false);
			}
		}
		
		override public function onTick(time:Number):void {
			if (controller.moving) {
				if (controller.onWall && (controller.direction == controller.wallingDirection)) {
					setState(WallPushingState);
					setPlayerMovieClip(renderTarget.standing, false);
					return;
				}
				
				if (!controller.dashing) {
					setState(WalkingState);
					setPlayerMovieClip(renderTarget.moving, true);
					return;	
				}
				
				var vel:Vec2D = getVelocity();
				
				if (vel.y > 0 && !controller.onFloor) {
					setState(FallingDownDashingState);
					setPlayerMovieClip(renderTarget.fallingDown, false);
					return;
				}
				
				vel.x = controller.direction * PlayerController.RUNNING_SPEED;
				vel.y += PlayerController.GROUND_PUSHING_FORCE;
				setVelocity(vel);
				updateRenderTargetDirection();
			} else {
				setState(StandingState);
				setPlayerMovieClip(renderTarget.standing, false);
			}
		}
	}
}